import com.hanxu.dao.UserMapper;
import com.hanxu.pojo.User;
import com.hanxu.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Mytest {

    public static void main(String[] args) {
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();

        for (User user : userList){
            System.out.println(user);
        }

        sqlsession.close();
    }

    @Test
    public void test1(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        User user = mapper.getUserById(1);

        System.out.println(user);


        sqlsession.close();

    }

    @Test
    public void test2(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        int user = mapper.addUser(new User(9,"hhh","xxx","13277777","sshsss","nann","ddd","ddd"));

        System.out.println(user);

        //注意提交事务，不然数据库没有数据
        sqlsession.commit();

        sqlsession.close();

    }

    @Test
    public void test3(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        int i = mapper.updateUser(new User(9, "222", "333", "444", "555", "", "", ""));
        System.out.println(i);
        sqlsession.commit();
        sqlsession.close();

    }

    @Test
    public void test4(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        int i = sqlsession.getMapper(UserMapper.class).deleteUserById(9);
        System.out.println(i);
        sqlsession.commit();

        sqlsession.close();

    }

    /**
     * 万能的map传递参数
     */
    @Test
    public void test5(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);

       Map<String,Object> map = new  HashMap<String,Object>();

       map.put("id",9);
       map.put("userName","hhhhh");
       map.put("passWord","hhhhh");
       map.put("phone","1322851");

       mapper.addUser2(map) ;
       sqlsession.commit();

        sqlsession.close();

    }


    /**
     * 模糊查找一个用户
     */
    @Test
    public void getUserLike(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);

        //注意这里的 %% 号
        List<User> userLike = mapper.getUserLike("%h%");

        //iter 快捷键
        for (User user : userLike) {
            System.out.println(user);
        }


        sqlsession.close();

    }


}
